*****************
* SUMMARY STATS *
*****************
*Outcomes
local outcomes_lr="fin_health_b1_8 VANTAGE_V3_SCORE_b1_8 zerorevolving_b1_8 ALL5042_b1_8 ANY_AUT_OR_LEASE_b1_8"

use unique_id RECORD_NB DEM_ZIP_CD_b1 `outcomes_lr' fem1_exp ageatcase evicted  using "${build}/iv_fin_strain.dta", clear
bys unique_id: keep if _n==1
gen evict_case_sample=1
*Rename baseline zipcode to match placebo group name
rename DEM_ZIP_CD_b1  DEM_ZIP_CD

append using "${build}/experian_panel.dta", force keep(RECORD_NB DEM_ZIP_CD fem1_exp  ageatcase  `outcomes_lr' )
replace evict_case_sample=0 if evict_case_sample==.

************************************************************
*CREATE ZIP CODE WEIGHTS TO MATCH THE DISTRIBUTION OF CASES*
************************************************************
*CALC. WEIGHTS TO MATCH GEOGRAPHIC DIST. OF CASES *
*Step 0: Zero out missing zipcodes if any
drop if DEM_ZIP_CD==.

*Step 1: Get proportions of cases (and placebo individuals) in each zip
capture drop one
gen one=1

foreach samp of numlist 0 1 { 
	bys DEM_ZIP_CD: egen totcases_zip_`samp'=total(one) if  evict_case_sample==`samp'
	egen totcases_`samp'=total(one) if  evict_case_sample==`samp'
	gen prop_`samp'_0=totcases_zip_`samp'/totcases_`samp'
	bys DEM_ZIP_CD: egen prop_`samp'=median(prop_`samp'_0)
	drop prop_`samp'_0 totcases_`samp' totcases_zip_`samp'
}

*Step 2: Calculate weights 
capture drop wt
gen wt=1 if evict_case_sample==1
replace wt=prop_1/prop_0 if evict_case_sample==0

************************************
* IV ANALYSIS SAMPLE + RANDOM SAMP *
************************************
*Recode missing values for the summary stats
rename fem1_exp fem1
foreach var of varlist fem `outcomes_lr' {
	recode `var' (-1=.)
	
}

tab evict_case_sample

eststo clear
eststo sum1: estpost sum ageatcase fem1   ///
		`outcomes_lr'  if evicted==1 
		
eststo sum2: estpost  sum ageatcase fem1   ///
		`outcomes_lr'  if evicted==0 
		
eststo sum3: estpost sum ageatcase fem1       ///
		`outcomes_lr'  if evict_case_sample==0   [aw=wt]

*****************	
*EXPORT TO LATEX*
*****************
label var ageatcase "Age at filing"
label var fem1 "Female"
capture label var ALL5042_1_8 "Total balance: collections and delinquencies"

foreach v of varlist ageatcase fem1 ///
		`outcomes_lr' {
	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'	
}
	
esttab sum1 sum2 sum3 using "${Inputs}/Cook_experian_sum_stats.tex", main(mean %9.3fc) aux(sd %9.3fc) wide  replace   ///
	 label nonote not nonumber nodepvar booktabs  ///
    	refcat(ageatcase  "\\ \emph{Person characteristics}" ///
   fin_health_b1_8 " \\  \emph{Outcomes: Quarters 1-8 Before Filing}", nolabel) mtitles("Evicted" "Not Evicted" "Random sample") 
